package com.news.gpb.service.impl;

import com.news.gpb.dao.CollectDao;
import com.news.gpb.dao.LikeDao;
import com.news.gpb.dao.NewsDao;
import com.news.gpb.entity.Like;
import com.news.gpb.entity.News;
import com.news.gpb.entity.Coll;
import com.news.gpb.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("newsService")
public class NewsServiceImpl implements NewsService {
    
    @Autowired
    NewsDao newsDao;
    @Autowired
    LikeDao likeDao;
    @Autowired
    CollectDao collectDao;
    @Override
    public List<News> getNewsList(News news) {
        return newsDao.getNewsList(news);
    }

    public List<News> getAllList(News news) {
        return newsDao.getAllList(news);
    }

    public List<News> getHotList() {
        return newsDao.getHotList();
    }

    @Override
    public News getNewsDetail(int news_id) {
        return newsDao.getNewsDetail(news_id);
    }

    @Override
    public List<News> search(String keyword) {
        return newsDao.search(keyword);
    }

    @Override
    public Integer updateViews(News news) {
        return newsDao.updateViews(news);
    }

    @Override
    public String getLike(Like like) {
        Integer res = likeDao.getLike(like);
        if(res!=null&&res!=0)
        {
            System.out.println("exist");
            return "exist";
        }
        else return "null";
    }

    @Override
    public String likeUp(Like like) {
        Integer res = likeDao.likeUp(like);
        if(res!=null&&res!=0)
        {
            newsDao.updateLikesUp(like.getNews_id());
            return "ok";
        }
        else return "error";
    }

    @Override
    public String likeDown(Like like) {
        Integer res = likeDao.likeDown(like);
        if(res!=null&&res!=0)
        {
            newsDao.updateLikesDown(like.getNews_id());
            return "ok";
        }
        else return "error";
    }

    @Override
    public String getCollect(Coll coll) {
        Integer res =collectDao.getCollect(coll);
        if(res!=null&&res!=0)
        {
            return "existed";
        }
        else return "null";
    }

    @Override
    public String collectUp(Coll coll) {
        Integer res = collectDao.collectUp(coll);
        if(res!=null&&res!=0)
        {
            newsDao.updateCollectUp(coll.getNews_id());
            return "ok";
        }
        else return "error";
    }

    @Override
    public String collectDown(Coll coll) {
        Integer res = collectDao.collectDown(coll);
        if(res!=null&&res!=0)
        {
            newsDao.updateCollectDown(coll.getNews_id());
            return "ok";
        }
        else return "error";
    }

    @Override
    public List<News> getMyLike(int user_id) {
        List<Integer> likes = likeDao.getMyLike(user_id);
        List<News> newsList=new ArrayList<>();
        if(likes!=null)
        {
            for(int i:likes)
            {
                newsList.add(newsDao.getNewsById(i));
            }
        }
        return newsList;
    }

    @Override
    public List<News> getMyCollect(int user_id) {
        List<Integer> likes = collectDao.getMyCollect(user_id);
        List<News> newsList=new ArrayList<>();
        if(likes!=null)
        {
            for(int i:likes)
            {
                newsList.add(newsDao.getNewsById(i));
            }
        }
        return newsList;
    }
}
